##### generate supplementary results, one table per page

library(BBmisc)
library(xtable)
library(stringr)

library(devtools)
load_all("~/work/coseal/aslib-r/aslib/")

# FIXME: remove this when we have the steps in the scenario
source("~/work/coseal/aslib-r/server_scripts/eda_config.R")
confpath = "~/work/coseal/aslib-r/configs"

tmp = load2("~/work/coseal/aslib-r/server_scripts/llama_results.RData")
res = tmp$res

res = dropNamed(res, c("id", "repl", "llama.fun"))
res = sortByCol(res, c("prob", "algo", "model"))
colnames(res) = c("Scenario", "Type", "Model", "Solved", "PAR10", "MCP")


sink(file = "results_supplementary.tex")

cat(paste(sep = "\n",
  "\\documentclass[]{elsarticle}",
  "\\usepackage{caption}",
  "\\usepackage{booktabs}",
  "\\begin{document}"
))

scenarios = unique(res$Scenario)
for (i in seq_along(scenarios)) {
  scenario.id = scenarios[i]
  write(scenario.id, stderr())
  ast = parseASScenario(file.path("/home/larsko/work/coseal/aslib_data-aslib-v1.0.1", scenario.id))
  config = readEDAConfig(ast, confpath)
  steps = make.names(strsplit(ast$desc$default_steps, ", ")[[1]])
  r = subset(res, Scenario == scenario.id)
  # sort vbs into first row
  r[c(1, 4), ] = r[c(4, 1), ]
  r$Scenario = NULL
  feats = getProvidedFeatures(ast, steps)
  costs = getCostsAndPresolvedStatus(ast, steps)$costs
  cap1 = sprintf("Full experimental results for scenario %s. The following feature steps were used: %s (%i features).",
    scenario.id, str_replace_all(collapse(steps, sep=", "), "_", "-"), length(feats))
  if (is.null(costs))
    cap2 = ""
  else
    cap2 = sprintf("The cost for using the feature steps (adapted for presolving) is %g, on average %g per instance.",
      sum(costs), mean(costs))
  xt = xtable(r, caption = paste(cap1, cap2, sep = "\n"))
  print(xt, "latex", include.rownames = FALSE, booktabs = TRUE)
}

cat(paste(sep = "\n",
  "\\end{document}"
))

sink(NULL)

